import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ShipperBaseService } from '@shared';
import { RebateManagementService } from '../../services/rebate-management.service';

@Component({
  selector: 'app-parter-channel-rebate-management-particulars',
  templateUrl: './particulars.component.html'
})
export class ParterRebateManageMentParticularsComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';
  _$expand = false;
  tabs = {
    payQuantity: 0,
    cancelQuantity: 0,
  };
  resourceStatus: any;
  data = this.service.$api_get_searchPageList;
  constructor(
    public service: RebateManagementService,
    public shipperservice: ShipperBaseService
  ) {}
  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
    /**
  * 伸缩查询条件
  */
     expandToggle(): void {
      this._$expand = !this._$expand;
      this.sf?.setValue('/_$expand', this._$expand);
    }
  /**
 * 查询参数
 */
   get reqParams() {
    const params: any = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return { 
      ...params,
      deadlineTime: {
        start: this.sf?.value?.deadlineTime?.[0] || '',
        end: this.sf?.value?.deadlineTime?.[1] || '',
      },
     };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        billCode: {
          type: 'string',
          title: '订单号'
        },
        orderPaymentCode: {
          type: 'string',
          title: '付款单号'
        },
        enterpriseInfoName: {
          type: 'string',
          title: '下单客户'
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            allowClear: true,
            asyncData: () => this.shipperservice.getNetworkFreightForwarder()
          }
        },
        partnerName: {
          type: 'string',
          title: '合伙人名称',
          ui: {
            visibleIf: {
              _$expand: (value: boolean) => value
            },
          }
        },
        handlerTime: {
          title: '时间范围',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd',
            visibleIf: {
              _$expand: (value: boolean) => value
            },
            allowClear: true
          } as SFDateWidgetSchema
        }
      }
    };
    this.ui = {
      '*': {
        spanLabelFixed: 140,
        grid: { span: 8, gutter: 4 }
      }
    };
  }
  selectChange(e: number) {
    this.resourceStatus = e;
    this.initST();
    setTimeout(() => {
      if(e == 1) {
        this.data = this.service.$api_get_searchRefundPageList
      } else {
        this.data = this.service.$api_get_searchPageList
      }
      this.st.load();
    }, 500);
  }
  initST() {
    this.columns = [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return (this.st.pi * this.st.ps) - this.st.ps + idx + 1 + ''
        }
      },
      {
        title: '订单号',
        width: 150,
        index: 'billCode'
      },
      {
        title: '订单金额（元）',
        width: 150,
        index: 'orderAmount'
      },
      {
        title: '付款金额（元）',
        width: 150,
        index: 'payAmount'
      },
      {
        title: '预估返佣金额（元）',
        width: 150,
        index: 'profitAmount'
      },
      {
        title: '附加费率',
        width: 150,
        index: 'additionalRate'
      },
      {
        title: '下单客户',
        width: 150,
        index: 'enterpriseInfoName'
      },
      {
        title: '网络货运人',
        width: 180,
        index: 'ltdName'
      },
      {
        title: '销售渠道',
        width: 180,
        index: 'salesmanName'
      },
      {
        title: '合伙人名称',
        width: 180,
        index: 'partnerName'
      },
      {
        title: '合伙人等级',
        width: 150,
        index: 'partnerLevelName'
      },
      {
        title: '管理费比例',
        width: 150,
        index: 'manageFee'
      },
      {
        title: '固定结算费率',
        width: 150,
        index: 'settlementFee'
      },
      {
        title: '返佣时间',
        width: 180,
        index: 'returnCommissionTime'
      }
    ];
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.st.load(1);
  }
}
